package com.vogue.flowmeter.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.vogue.flowmeter.profile.BleProfileService;
import com.vogue.flowmeter.ui.activity.MainActivity;


public abstract class BaseFragment extends Fragment{
    /**
     * 当fragment与activity发生关联时调用
     * @param context  与之相关联的activity
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);


    }

    /**
     *
     * @param msg
     */
    protected void showToast(String msg){
        if (true){
            requireActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(requireActivity(),msg,Toast.LENGTH_LONG).show();
                }
            });
        }
    }

    /**
     *
     * @return
     */
    public BleProfileService.LocalBinder getLocalBinder() {
        Log.e(BaseFragment.class.getName(),"getLocalBinder");
        hideKeyboard(((MainActivity)requireActivity()));
        return  ((MainActivity)requireActivity()).localBinder.isReady() ? ((MainActivity)requireActivity()).localBinder:null;
    }

    /**
     *
     * @param activity
     */
    public static void hideKeyboard(Activity activity) {
        View view = activity.findViewById(android.R.id.content);
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     *
     * @param activity
     */
    public static void showKeyboard(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }
}
